import request from '@/utils/request';
import { ElMessage } from 'element-plus';

// 创建会诊
export function createConsultation(consultationDO) {
    return request({
        url: '/interaction/createConsultation',
        method: 'post',
        data: consultationDO
    }).then(response => {
        console.log('创建会诊接口响应:', response);
        if (response.code === '000') {
            ElMessage.success('会诊创建成功');
            return response;
        } else {
            ElMessage.error('会诊创建失败: ' + response.msg);
            throw new Error(response.msg);
        }
    }).catch(error => {
        console.error('创建会诊失败:', error);
        ElMessage.error('创建会诊失败: ' + error.message);
        throw error;
    });
}

// 获取会诊
export function getConsultation(consultationId) {
    return request({
        url: '/interaction/getConsultation',
        method: 'get',
        params: {
            consultationId: consultationId
        }
    }).then(response => {
        console.log('获取会诊接口响应:', response);
        if (response.code === '000') {
            return response;
        } else {
            ElMessage.error('获取会诊失败: ' + response.msg);
            throw new Error(response.msg);
        }
    }).catch(error => {
        console.error('获取会诊失败:', error);
        ElMessage.error('获取会诊失败: ' + error.message);
        throw error;
    });
}

// 发送消息
export function sendMessage(messageData) {
    return request({
        url: '/interaction/sendMessage',
        method: 'post',
        data: messageData
    }).then(response => {
        console.log('发送消息接口响应:', response);
        if (response.code === '000') {
            ElMessage.success('消息发送成功');
            return response;
        } else {
            ElMessage.error('消息发送失败: ' + response.msg);
            throw new Error(response.msg);
        }
    }).catch(error => {
        console.error('发送消息失败:', error);
        ElMessage.error('发送消息失败: ' + error.message);
        throw error;
    });
}

// 接收消息
export function receiveMessage(consultationId) {
    return request({
        url: '/interaction/receiveMessage',
        method: 'get',
        params: {
            consultationId: consultationId
        }
    }).then(response => {
        console.log('接收消息接口响应:', response);
        if (response.code === '000') {
            return response;
        } else {
            ElMessage.error('接收消息失败: ' + response.msg);
            throw new Error(response.msg);
        }
    }).catch(error => {
        console.error('接收消息失败:', error);
        ElMessage.error('接收消息失败: ' + error.message);
        throw error;
    });
}

// 获取会诊列表
export function getConsultationList(userId, role) {
    return request({
        url: '/interaction/getConsultationList',
        method: 'post',
        data: {
            userId: userId,
            role: role
        }
    }).then(response => {
        console.log('获取会诊列表接口响应:', response);
        if (response.code === '000') {
            return response;
        } else {
            ElMessage.error('获取会诊列表失败: ' + response.msg);
            throw new Error(response.msg);
        }
    }).catch(error => {
        console.error('获取会诊列表失败:', error);
        ElMessage.error('获取会诊列表失败: ' + error.message);
        throw error;
    });
}

// 结束会诊
export function endConsultation(consultationId) {
    return request({
        url: '/interaction/endConsultation',
        method: 'put',
        params: {
            consultationId: consultationId
        }
    }).then(response => {
        console.log('结束会诊接口响应:', response);
        if (response.code === '000') {
            ElMessage.success('会诊已成功结束');
            return response;
        } else {
            ElMessage.error('结束会诊失败: ' + response.msg);
            throw new Error(response.msg);
        }
    }).catch(error => {
        console.error('结束会诊失败:', error);
        ElMessage.error('结束会诊失败: ' + error.message);
        throw error;
    });
}